package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    @RequestMapping("/findAll")
    public Result findAll() {
        List<Menu> all = menuService.findAll();
        if (all != null && all.size() > 0) {
            return new Result(true,"菜单查询成功", all);
        } else {
            return new Result(false, "菜单查询失败");
        }
    }
    @PreAuthorize("hasAuthority('MENU_QUERY')")//查询菜单权限
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return menuService.findParent(queryPageBean);
    }

    @RequestMapping("/findChildrenById")
    public List<Menu> findChildrenById(Integer parentId) {
        return menuService.findChildrenById(parentId);
    }

    @RequestMapping("/findById")
    public Result findById(Integer menuId) {
        try {
            Menu menu = menuService.findById(menuId);
            return new Result(true, "菜单查询成功", menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "菜单查询失败");
        }
    }
    @PreAuthorize("hasAuthority('MENU_ADD')")//新增菜单权限
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu) {

        try {
            menuService.add(menu);
            return new Result(true, "菜单增加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "菜单增加失败");
        }
    }
    @PreAuthorize("hasAuthority('MENU_EDIT')")//编辑菜单权限
    @RequestMapping("/edit")
    public Result edit(@RequestBody  Menu menu) {
        System.out.println("www"+menu);
        try {
            menuService.edit(menu);
            return new Result(true, "菜单编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "菜单编辑失败");
        }
    }
    @PreAuthorize("hasAuthority('MENU_DELETE')")//删除菜单权限
    @RequestMapping("/delete.do")
    public Result delete(Integer id) {
        try {
            menuService.delete(id);
        }  catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "菜单删除失败");
        }
        return new Result(true, "菜单删除成功");
    }

    @RequestMapping("/findMenuByUser")
    public Result findMenuByUser(String username) {

        try {
            List<Map> list=menuService.findMenuByUser(username);
            return new Result(true, "菜单查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "菜单查询失败");
        }

    }





    @RequestMapping("/findPermissionIdsByMenuId")
    public Result findPermissionIdsByMenuId(Integer menuId) {
        try {
            List<Integer> menuIds = menuService.findPermissionIdsByMenuId(menuId);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }



}
